﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Commons.Data;

namespace Test_EF_DbContext.Controllers
{

    #region 数据层
    /// <summary>
    /// 领域数据层基类
    /// </summary>
    public class DbContextTestBase : DbContextRepositoryBase
    {
        public DbContextTestBase()
            : base(new testEntities())
        { }
    }
    #endregion


    public class EFController : Controller
    {
        //
        // GET: /EF/

        public ActionResult Index()
        {
            return View();
        }

        #region 编辑列表
        public ActionResult EditList()
        {
            List<User> userList = new DbContextTestBase().GetEntities<User>().Take(5).ToList();
            return View(userList);
        }
        [HttpPost]
        public ActionResult EditList(FormCollection form)
        {
            List<User> userList = new List<User>();
            string[] status = form.GetValues("Status");
            string[] id = form.GetValues("ID");
            int j = 0;
            form.GetValues("Name").ToList().ForEach(i =>
            {
                userList.Add(new User
                {
                    ID = Convert.ToInt32(id[j]),
                    Name = i,
                    Status = Convert.ToInt32(status[j]),
                    //User_Profile = new User_Profile { ID = Convert.ToInt32(id[j]), }
                });
                j++;
            });
            new DbContextTestBase().Update<User>(userList);

            //userRepository.Update<User>(userList);
            return View(new DbContextTestBase().GetEntities<User>().Take(5).ToList());
        }

        #endregion
    }
}
